﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace GameNodeProgramm
{
    class GameNode
    {
        public int X { get; private set; }
        public int Y { get; private set; }
        public char Symbol { get; private set; }

        public GameNode(int x, int y, char symbol)
        {
            X = x;
            Y = y;
            Symbol = symbol;
        }

        public void MoveUp()
        {
            Y--;
        }

        public void MoveDown()
        {
            Y++;
        }

        public void MoveLeft()
        {
            X--;
        }

        public void MoveRight()
        {
            X++;
        }

        public virtual void Move(Func<int, int, Position> positionFunc)
        {
            Random rnd = new Random();
            switch (rnd.Next(1, 5))
            {
                case 1:
                    {
                        if (positionFunc(X, Y - 1) == Position.Free)
                        {
                            MoveUp();
                        }
                        break;
                    }
                case 2:
                    {
                        if (positionFunc(X, Y + 1) == Position.Free)
                        {
                            MoveDown();
                        }
                        break;
                    }
                case 3:
                    {
                        if (positionFunc(X - 1, Y) == Position.Free)
                        {
                            MoveLeft();
                        }
                        break;
                    }
                case 4:
                    {
                        if (positionFunc(X + 1, Y) == Position.Free)
                        {
                            MoveRight();
                        }
                        break;
                    }
            }
        }
    }

    class ConsoleNode : GameNode
    {
        public ConsoleColor NodeColor { get; private set; }

        public ConsoleNode(int x, int y, char nodeSymbol, ConsoleColor nodeColor) : base(x, y, nodeSymbol)
        {
            NodeColor = nodeColor;
        }
    }

    class CustomerNode : ConsoleNode
    {
        public CustomerNode(int x, int y, char nodeSymbol, ConsoleColor nodeColor) : base(x, y, nodeSymbol, nodeColor)
        {  }

        CurrentPosition position = CurrentPosition.Field;
        bool changeRow = false;
        bool moveOnTop = false;
        List<int> coordX;
        List<int> coordY;
        
        enum CurrentPosition
        {
            Up,
            Down,
            Left,
            Right,
            Field
        }

        protected void checkInitialPosition (Func<int,int, Position> initialPosition)
        {
            if (initialPosition(X - 1, Y) == Position.Border)
            { position = CurrentPosition.Left; }
            if (initialPosition(X + 1, Y) == Position.Border)
            { position = CurrentPosition.Right; }
            if (initialPosition(X, Y - 1) == Position.Border)
            { position = CurrentPosition.Up; }
            if (initialPosition(X, Y + 1) == Position.Border)
            { position = CurrentPosition.Down; }
        }

        public override void Move(Func<int, int, Position> positionFunc)
        {
            if (position != CurrentPosition.Left & positionFunc(X-1, Y) != Position.Border && !changeRow && !moveOnTop)
            {
                if (positionFunc(X - 1, Y) == Position.Free)
                {
                    MoveLeft();
                    if (positionFunc(X-1, Y) == Position.Border)
                    {
                        position = CurrentPosition.Left;
                    }
                }
            }
            else if (position == CurrentPosition.Left && positionFunc(X, Y+1) != Position.Border && !changeRow && !moveOnTop)
            {
                if (positionFunc(X, Y+1) == Position.Free)
                {
                    MoveDown();
                    changeRow = true;
                }
            }
            else if (position != CurrentPosition.Right & positionFunc(X + 1, Y) != Position.Border && changeRow && !moveOnTop)
            {
                if (positionFunc(X + 1, Y) == Position.Free)
                {
                    MoveRight();
                    if (positionFunc(X + 1, Y) == Position.Border)
                        position = CurrentPosition.Right;
                }
            }
            else if (position == CurrentPosition.Right && positionFunc(X, Y + 1) != Position.Border && changeRow && !moveOnTop)
            {
                if (positionFunc(X, Y + 1) == Position.Free)
                {
                    MoveDown();
                    changeRow = false;
                }
            }
            else if ((position == CurrentPosition.Right && positionFunc(X, Y + 1) == Position.Border && positionFunc(X + 1, Y) == Position.Border) ||
                (position == CurrentPosition.Left && positionFunc(X, Y + 1) == Position.Border && positionFunc(X - 1, Y) == Position.Border))
            {
                moveOnTop = true;
            }
            if (moveOnTop)
            {
                if (positionFunc(X, Y - 1) == Position.Free)
                {
                    MoveUp();
                    if (positionFunc(X, Y - 1) == Position.Border)
                    {
                        moveOnTop = false;
                        changeRow = false;
                    }
                }
            }
        //    if (position == CurrentPosition.Right & positionFunc(X - 1, Y) != Position.Border && !changeRow && !moveOnTop)
        //    {

        //        if (positionFunc(X - 1, Y) == Position.Free)
        //        {
        //            MoveLeft();
        //            if (positionFunc(X - 1, Y) == Position.Border)
        //            {
        //                position = CurrentPosition.Left;
        //            }
        //        }
        //    }
        }
    }
}
